/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "TextureBase.h"
#include <list>
#include <logs/Log.h>

namespace liba
{
namespace lib3d
{
namespace hard
{
	
#ifdef _DEBUG
	static std::list<TextureBase *> all_nodes;
#endif	

#if defined(IPHONE_VERSION) || defined(POW2_TEXTURE)
static unsigned int nextPOT(unsigned int x)
{
	x = x - 1;
	x = x | (x >> 1);
	x = x | (x >> 2);
	x = x | (x >> 4);
	x = x | (x >> 8);
	x = x | (x >> 16);
	return x + 1;
}
#endif
	
TextureBase::TextureBase(int mipmap_levels, const pictureformat::Format & hardware_format, int _width, int _height):
mipmap_levels( mipmap_levels ),
#if defined(IPHONE_VERSION) || defined(POW2_TEXTURE)
width( nextPOT(_width) ),
height( nextPOT(_height) ),
original_width(_width),
original_height(_height),
u_scale( float(_width) / float(nextPOT(_width)) ),
v_scale( float(_height) / float(nextPOT(_height)) ),
#else
width( _width ),
height( _height ),
#endif
hardware_format( hardware_format )
{
#if defined(IPHONE_VERSION) || defined(POW2_TEXTURE)
	if(width != _width || height != _height)
		logs::log() << "WARNING: used non power-of-two texture!\n";
#endif
#ifdef _DEBUG
	all_nodes.push_back(this);
#endif
}

TextureBase::~TextureBase()
{
#ifdef _DEBUG
	all_nodes.remove(this);
#endif
}
	
void TextureBase::report_usage()
{
#ifdef _DEBUG
	logs::log() << "total textures: " << (int)all_nodes.size() << "\n";
#endif
}

} // namespace hard
} // namespace lib3d
} // namespace liba
